﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace k3cloud_webapi_sample
{
    public class JsonBaseClass
    {
        public static T DeserrializeJsonToObject<T>(string json) where T : class
        {
            try
            {
                JsonSerializer serializer = new JsonSerializer();
                StringReader reader = new StringReader(json);
                return serializer.Deserialize(new JsonTextReader(reader), typeof(T)) as T;
            }
            catch (Exception) { return null; }
        }
        public static string SerializeObjectToJson(object obj)
        {
            string json = string.Empty;
            try
            {
                JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings();
                jsonSerializerSettings.NullValueHandling = NullValueHandling.Ignore;
                json = JsonConvert.SerializeObject(obj, Formatting.Indented, jsonSerializerSettings);
            }
            catch (Exception)
            {
                json = null;
            }
            return json;
        }
    }
}
